package com.qf.encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    //“毛泽东”编码之后 = %E6%AF%9B%E6%B3%BD%E4%B8%9C
    private String path = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=毛泽东&rsv_pq=be1c925a00000adb&rsv_t=1386DpKrDTmRabMu8OQMz8HiKLpQVm5Ro2D23oIr3TBKkjOQAEugmzNYGmQ&rqlang=cn&rsv_enter=1&rsv_sug3=7&rsv_sug1=2&rsv_sug7=100";




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //对网址进行编码
        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        path = path+encode+"s";

        Log.d("flag", "---------->onCreate: 进行编码之后的内容" +encode);

        //解码  ： 怒发冲冠凭栏处
        String decode = null;
        try {
            decode = URLDecoder.decode("%E6%80%92%E5%8F%91%E5%86%B2%E5%86%A0%E5%87%AD%E6%A0%8F%E5%A4%84","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "---------->onCreate: 解密后的内容是：" +decode);


        new Thread(new Runnable() {
            @Override
            public void run() {

                InputStream is = null;
                ByteArrayOutputStream baos = null;

                try {
                    URL url = new URL(path);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(5000);

                    if(httpURLConnection.getResponseCode() == 200){
                        is = httpURLConnection.getInputStream();
                        baos = new ByteArrayOutputStream();

                        int len = 0;
                        byte[] buf = new byte[1024*3];

                        while((len = is.read(buf))!=-1){
                            baos.write(buf,0,len);
                        }

                        Log.d("flag", "---------->run: " +new String(baos.toByteArray()));
                    }

                } catch (MalformedURLException e) {
                    Log.d("flag", "---------->run: MalformedURLException" );
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d("flag", "---------->run: IOException" );
                    e.printStackTrace();
                }finally{
                    Log.d("flag", "---------->run: finally");
                    if(is!=null){
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if(baos!=null){
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();
    }


}
